package com.chaoyong.higo.activity;

import java.io.UnsupportedEncodingException;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.MyCountTimer;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class FrogetPwdActivity extends BaseActivity {

	private Activity context;
	private EditText et_phone,et_code;
	private Button submit;
	private TextView tv_code;
	private MyCountTimer timer;
	
	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String phone = et_phone.getText().toString();
		String code = et_code.getText().toString();
		switch (v.getId()) {
		case R.id.base_left_iv:// 返回
			this.finish();
			break;
		case R.id.forget_get_code:
			if(TextUtils.isEmpty(phone)){
				showToast("请输入手机号码！");
				return;
			}
			getCode(phone);
			break;
		case R.id.forget_submit:
			if(TextUtils.isEmpty(phone)){
				showToast("请输入手机号码！");
				return;
			}
			if(TextUtils.isEmpty(code)){
				showToast("请输入验证码！");
				return;
			}
			verificationCode(phone, code);
			break;
		default:
			break;
		}
	}

	private void verificationCode(final String phone, String code) {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		try {
			json.put("phone", phone);
			json.put("code", code);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, OApi.Appport_backCode, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					if(jso.getInt("status") == 1){
						Intent intent = new Intent(context, ForgetPwdActivity.class);
						intent.putExtra("phone", phone);
						startActivity(intent);
						showToast("验证码正确！");
					}else{
						showToast("验证码不正确！");
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	private void getCode(String phone) {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		try {
			json.put("phone", phone);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestParams params = new RequestParams();
		try {
			params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.POST, OApi.Appport_checkedPhone, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				showToast(arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject jso = new JSONObject(result);
					if(jso.getInt("status") == 1){
						showToast("验证码发送成功！");
						timer.start();
					}else{
						showToast(jso.getString("data"));
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public int layoutResID() {
		// TODO Auto-generated method stub
		return R.layout.activity_forget_pwd01;
	}

	@Override
	public void findView() {
		// TODO Auto-generated method stub
		context = this;
		et_phone = (EditText) findViewById(R.id.forget_phone);
		et_code = (EditText) findViewById(R.id.forget_code);
		tv_code = (TextView) findViewById(R.id.forget_get_code);
		submit = (Button) findViewById(R.id.forget_submit);
		
		base_left_iv = (LinearLayout) findViewById(R.id.base_left_iv);
		base_title_tv = (TextView) findViewById(R.id.base_title_tv);
		base_right_tv = (TextView) findViewById(R.id.baseduobao_right_tv);
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		tv_code.setOnClickListener(this);
		submit.setOnClickListener(this);
		base_left_iv.setOnClickListener(this);
		
		//base_right_tv.setVisibility(View.GONE);
		base_title_tv.setText("忘记密码");
		base_right_tv.setVisibility(View.GONE);
		
		timer = new MyCountTimer(tv_code, 0xfff30008, 0xff969696);
	}
	

}
